<template>
  <el-dialog
    :title="!dataForm.attrId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
    <el-form-item label="属性名" prop="attrName">
      <el-input v-model="dataForm.attrName" placeholder="属性名"></el-input>
    </el-form-item>
    <el-form-item label="属性图标" prop="icon">
      <el-input v-model="dataForm.icon" placeholder="属性图标"></el-input>
    </el-form-item>

    <el-form-item label="属性类型" prop="attrType">
      <el-select v-model="dataForm.attrType" placeholder="请选择">
        <el-option label="规格参数" value="1"> </el-option>
        <el-option label="销售属性" value="0"> </el-option>
      </el-select>
    </el-form-item>

    <el-form-item label="值类型" prop="valueType">
      <el-switch v-model="dataForm.valueType" 
        active-color="#13ce66" inactive-color="#ff4949" 
        active-text="多个值"
        inactive-text="只能单个值"
        :active-value="1"
        :inactive-value="0">
      </el-switch>
    </el-form-item>

    <el-form-item label="可选值" prop="valueSelect">
      <!-- allow-create 可以创建并选中选项中不存在的条目
           multiple属性即可启用多选  
           filterable属性即可启用搜索功能
           default-first-option属性，在该属性打开的情况下，按下回车就可以选中当前选项列表中的第一个选项
       -->
      <el-select
          v-model="dataForm.valueSelect"
          multiple
          filterable
          allow-create
          default-first-option
          placeholder="请输入内容"
        ></el-select>
    </el-form-item>
    


    <el-form-item label="所属分类" prop="catelogId">
      <category-cascader :catelogPath.sync="catelogIds" ></category-cascader>
    </el-form-item>

    <el-form-item label="所属分组" prop="attrGroupId">
      <el-select v-model="dataForm.attrGroupId" filterable placeholder="请输入内容" >
        <el-option v-for="item in attrGroupList" 
            :key="item.attrGroupId" 
            :label="item.attrGroupName" 
            :value="item.attrGroupId"> </el-option>
      </el-select>
    </el-form-item>

    <el-form-item label="是否需要检索" prop="searchType">
      <el-switch v-model="dataForm.searchType" active-color="#13ce66" inactive-color="#ff4949" :active-value="1" :inactive-value="0"></el-switch>
    </el-form-item>
    <el-form-item label="快速展示" prop="showDesc">
      <el-switch v-model="dataForm.showDesc" active-color="#13ce66" inactive-color="#ff4949" :active-value="1" :inactive-value="0"></el-switch>
    </el-form-item>
    <el-form-item label="启用状态" prop="enable">
      <el-switch v-model="dataForm.enable" active-color="#13ce66" inactive-color="#ff4949" :active-value="1" :inactive-value="0"></el-switch>
    </el-form-item>

    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import CategoryCascader from "../common/category-cascader";
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          attrId: 0,
          attrName: '',
          searchType: '',
          icon: '',
          valueType: '',
          valueSelect: [],
          attrType: '',
          enable: '',
          catelogId: '',
          attrGroupId : '', //三级分类所属的分组 
          showDesc: '',
        },
        catelogIds: [],  //分类选择框需要绑定数组
        attrGroupList:[],//三级分类关联的分组
        dataRule: {
          attrName: [
            { required: true, message: '属性名不能为空', trigger: 'blur' }
          ],
          // icon: [
          //   { required: true, message: '属性图标不能为空', trigger: 'blur' }
          // ],
          valueSelect: [
            { required: true, message: '可选值列表不能为空', trigger: 'blur' }
          ],
          attrType: [
            { required: true, message: '属性类型不能为空', trigger: 'blur' }
          ],
          catelogId: [
            { required: true, message: '所属分类不能为空', trigger: 'blur' }
          ],
          // attrGroupId: [
          //   { required: true, message: '所属分组不能为空', trigger: 'blur' }
          // ]
        }
      }
    },
    components: {
      CategoryCascader
    },
    props:{
      type:{
        type: Number,
        default: 1
      }
    },
    watch:{
      //监听所属分类的变化  attrgroup-add-or-update中使用 @change 来实现
      catelogIds(path){
        //console.log("path: ",path);
        this.dataForm.catelogId = path[path.length - 1];

        this.$http({
            url: this.$http.adornUrl("/product/attrgroup/list"),
            method: 'get',
            params: this.$http.adornParams({
              catelogId: this.dataForm.catelogId
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.attrGroupList = data.data;
            }
          });
      }
    },
    methods: {
      init (id) {
        this.dataForm.attrId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.attrId) {
            this.$http({
              url: this.$http.adornUrl(`/product/attr/info/${this.dataForm.attrId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.attrName = data.data.attrName
                this.dataForm.searchType = data.data.searchType
                this.dataForm.icon = data.data.icon
                this.dataForm.valueSelect = data.data.valueSelect.split(",");
                this.dataForm.attrType = data.data.attrType
                this.dataForm.enable = data.data.enable
                this.dataForm.catelogId = data.data.catelogId
                this.dataForm.showDesc = data.data.showDesc
                this.catelogIds =  data.data.catelogPath

                this.$nextTick(() => {
                  this.dataForm.attrGroupId = data.data.attrGroupId;
                });
              }
            });
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/product/attr/${!this.dataForm.attrId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'attrId': this.dataForm.attrId || undefined,
                'attrName': this.dataForm.attrName,
                'searchType': this.dataForm.searchType,
                'icon': this.dataForm.icon,
                'valueSelect': this.dataForm.valueSelect.join(","),
                'attrType': this.dataForm.attrType,
                'enable': this.dataForm.enable,
                'catelogId': this.dataForm.catelogId,
                'showDesc': this.dataForm.showDesc,
                'attrGroupId': this.dataForm.attrGroupId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
